<?php

namespace Framework\support;
use Pimple\Container;

/**
 * 单例模式服务容器
 * Class ServiceContainer
 * @package Framework\support
 * @author 飞鸟与鱼
 */
class ServiceContainer extends Container
{
    protected $providers = [];

    private static $instance;


    public function __construct(array $values = [])
    {
        parent::__construct($values);
        $this->registerProviders();
    }


    public function getProviders()
    {
        return array_merge([
            ServiceProvider::class
        ], $this->providers);
    }

    public function registerProviders()
    {
        foreach($this->getProviders() as $provider) {
            parent::register(new $provider);
        }
    }

    // 防止复制此容器
    public function __clone()
    {
    }

    public static function getInstance()
    {
        if (!(self::$instance instanceof self)){
            self::$instance = new self();
        }
        return self::$instance;
    }
}